package com.xxh.utils;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 自定义工具类
 */
public class MqUtils {

    public static ConnectionFactory connectionFactory;

    static {
        connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/ems");
        //设置用户名，密码
        connectionFactory.setUsername("xiong");
        connectionFactory.setPassword("hanhan123");
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection=connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void close(Connection connection, Channel channel){
        try {
            if(channel!=null&&connection!=null){
                channel.close();
                connection.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }
}
